#include<iostream>
using namespace std;
const int N = 3e4 + 10;
int f[N], size_1[N];
int d[N];
int t, n;
int find(int x)
{
	if (x != f[x]) {
		int root = find(f[x]);
		d[x] +e= d[f[x]];
		f[x] = root;
	}
	return f[x];
}
int main()
{
	cin >> t;
	for (int i = 1; i <= N; i++) {
		f[i] = i;
		size_1[i] = 1;
	}
	while (t--) {
		char op[2];
		int a, b;
		cin >> op >> a >> b;
		int pa = find(a);
		int pb = find(b);
		if (op[0] == 'M') {
			f[pa] = pb;
			d[pa] = size_1[pb];
			size_1[pb] += size_1[pa];
		}
		else {	
			if (pa != pb) {
				cout << -1 << endl;
			}
			else {
				int ma = max(0, abs(d[a] - d[b]) - 1);
				cout << ma << endl;
			}
		}
	}
	return 0;
}